package com.knshare.utils;

import com.knshare.utils.domain.StartEndDate;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 * @author 杨静 1475654312@qq.com
 */
public class DateUtils {

    /**
     * 年-月-日
     */
    public static String YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 年-月-日 时:分:秒
     */
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return 当前日期
     */
    public static String getNowDate() {
        return parseNowDateFormat(YYYY_MM_DD);
    }

    /**
     * 获取当前日期时间, 默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @return 当前日期时间
     */
    public static String getNowDateTime() {
        return parseNowDateFormat(YYYY_MM_DD_HH_MM_SS);
    }


    /**
     * 获取当前格式化时间
     *
     * @param format 时间格式
     * @return 当前格式化时间
     */
    public static String parseNowDateFormat(String format) {
        return parseDateFormat(format, new Date());
    }

    /**
     * 时间格式化
     *
     * @param format 时间格式
     * @param date 时间
     * @return 格式化时间
     */
    public static String parseDateFormat(String format, Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取近几天
     *
     * @param currentDate 当前日期
     * @param recentDays 近几天
     * @param format 日期格式
     * @return 日期列表
     */
    public static List<String> getRecentDays(Date currentDate, int recentDays, String format) {
        List<String> dateList = new ArrayList<>();
        for (int i = 0; i < recentDays; i++) {
            Date date = org.apache.commons.lang3.time.DateUtils.addDays(currentDate, -i);
            String formatDate = parseDateFormat(format,date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    /**
     * 获取近几周
     *
     * @param localDate 当前周
     * @param recentWeeks 近几周
     * @return 周列表
     */
    public static List<String> getRecentWeeks(LocalDate localDate, int recentWeeks) {
        List<String> RecentWeeks = new ArrayList<>();
        List<StartEndDate> sevenWeekDate = getRecentWeeksDate(localDate, recentWeeks);
        for (StartEndDate startEndDate : sevenWeekDate) {
            LocalDate startDate = startEndDate.getStartDate();
            WeekFields weekFields = WeekFields.of(java.time.DayOfWeek.SUNDAY, 3);
            int weekNumber = startDate.get(weekFields.weekOfWeekBasedYear());
            int yearNumber = startDate.get(weekFields.weekBasedYear());
            String week = yearNumber+"/"+weekNumber;
            RecentWeeks.add(week);
        }
        return RecentWeeks;
    }

    /**
     * 获取近几周日期
     *
     * @param localDate 当前周
     * @param recentWeeks 近几周
     * @return 周日期列表
     */
    public static List<StartEndDate> getRecentWeeksDate(LocalDate localDate, int recentWeeks) {
        List<StartEndDate> sevenWeekDate = new ArrayList<>();
        for (int i = 0; i < recentWeeks; i++) {
            StartEndDate week = new StartEndDate();
            LocalDate startDate = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)).minusWeeks(i);
            LocalDate endDate = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)).minusWeeks(i);
            week.setStartDate(startDate);
            week.setEndDate(endDate);
            sevenWeekDate.add(week);
        }
        return sevenWeekDate;
    }

    /**
     * 获取近几月
     *
     * @param localDate 当前日期
     * @param recentMonths 近几月
     * @param format 格式化
     * @return 月列表
     */
    public static List<String> getRecentMonths(LocalDate localDate, int recentMonths, String format){
        if (StringUtils.isEmpty(format)) {
            format = "yyyy/MM";
        }
        List<String> months = new ArrayList<>();
        for (int i = 0; i < recentMonths; i++) {
            months.add(localDate.minusMonths(i).format(DateTimeFormatter.ofPattern(format)));
        }
        return months;
    }

    /**
     * 获取月份的开始日期和结束日期
     *
     * @param month 月份
     * @return 开始日期和结束日期
     */
    public static StartEndDate getMonthStartEndDate(String month){
        YearMonth yearMonth = YearMonth.parse(month);
        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();
        StartEndDate startEndDate = new StartEndDate();
        startEndDate.setStartDate(startDate);
        startEndDate.setEndDate(endDate);
        return startEndDate;
    }
}
